﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Model;
using BllFactory;
using IBLL;
using ETWebApi2.Models;

namespace ETWebApi2.Controllers
{
    [JwtAuthActionFilter]
    public class DetailController : ApiController
    {
        IFiles_tbService fileService = ServiceFactory.CreatService<IFiles_tbService>();
        ICUser_tbService cuserService = ServiceFactory.CreatService<ICUser_tbService>();
        IFileDownLoadLog_tbService filedg = ServiceFactory.CreatService<IFileDownLoadLog_tbService>();
        MyETLockContext db = DbContextFactory.GetDbContext();

       
        /// <summary>
        /// 获取详细
        /// </summary>
        /// <param name="id">文件Id</param>
        /// <returns></returns>
        [Route("Detail")]
        public HttpResponseMessage Get(int id)
        {
            Files_tb file = fileService.GetList(f => f.Id == id).FirstOrDefault();
            var tongji = (from filedown in db.FileDownLoad_tb
                          join cuser in db.CUser_tb
                          on filedown.DUser equals cuser.Id
                          where filedown.ActionState == 1 && filedown.FileId == id
                          select new
                          {
                              tongjiName = cuser.UserName
                          }).ToList();
            var names = (from cuser in db.CUser_tb
                        join customer in db.Customers_tb
                        on cuser.CompanyId equals customer.Id
                        where cuser.Id == file.UploadUserId
                        select new
                        {
                            userName=cuser.UserName,
                            companyName=customer.Name
                        }).ToList();
            string userNames = "";
            string canopenUser = "";
            string[] arr = file.CanOpenUser.Split('|');
            if(arr.Length>0 && !string.IsNullOrEmpty(arr[0]))  //不指定能打开文件的人时  默认所有人都能打开  arr有且只有一个的值是“”
                foreach (var a in arr)
                {
                    userNames += cuserService.GetList(c => c.Id == Convert.ToInt32(a)).FirstOrDefault().UserName;
                }
            if (userNames.Length < 2)
                canopenUser = "所有员工";
            else
                canopenUser = userNames.Substring(0, userNames.Length - 1);

            string toUsers = "";
            List<FileDownLoadLog_tb> filelg = filedg.GetList(f=>f.FileId==id);
            if(filelg.Count>0)
                foreach (var item in filelg)
                {
                    userNames+= cuserService.GetList(c => c.Id == item.DUser).FirstOrDefault().UserName+",";
                }
            if(userNames.Length>1)
                toUsers = userNames.Substring(0, userNames.Length - 1);
            return ObjectToJson.toJson(new { fileName = file.Attachment, time = file.Time, uploader = names[0].userName, companyName = names[0].companyName, caanopenUser = canopenUser, touser = toUsers,downNum=tongji.Count,tongji});
        }
    }
}
